import {
	getSpell
} from 'jian-pinyin';
import {
	Value
} from 'sass';
/**
 * 对联系人进行改变
 * @param {Object} userInfo 原来数组
 * @param {Object} arr 改变后的数组
 */
export function getAddressBooks(userInfo, arr) {
	userInfo.value.forEach(item => {
		let pinyin = getSpell(item.username).replace(/(\[|\])/g, "").toLocaleUpperCase().split(",")
		// 判断一个字符还是两个字符
		if (pinyin[0].length == 1) {
			if (pinyin[0].codePointAt() >= 65 && pinyin[0].codePointAt() <= 90) {
			addArr(arr, item, pinyin[0])
			}else {
				addArr(arr, item, "#")
			}
		} else {
			if (pinyin[0][0].codePointAt() >= 65 && pinyin[0][0].codePointAt() <= 90) {
				addArr(arr, item, pinyin[0][0])
			} else {
				addArr(arr, item, "#")
			}
		}
	})
	
}

function addArr(arr, user, val) {

	let index = arr.value.findIndex(item => item.title == val)
	if (index >= 0) {
		arr.value[index].userInfo.push(user)
	} else {
		arr.value.push({
			title: val,
			userInfo: [user]
		})
	}
}